package com.kmair.caiwu.financialexpress.util.impl;

import com.kmair.caiwu.financialexpress.pojo.ExpreseData;
import com.kmair.caiwu.financialexpress.pojo.FinancialExpressFormation;
import com.kmair.caiwu.financialexpress.pojo.Production;
import com.kmair.caiwu.financialexpress.pojo.SubjectParameter;
import com.kmair.caiwu.financialexpress.service.impl.ExpreseDataService;
import com.kmair.caiwu.financialexpress.service.impl.ProductionService;
import com.kmair.caiwu.financialexpress.service.impl.SubjectParameterService;
import com.kmair.caiwu.financialexpress.util.DataChangeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ExpressDataChange implements DataChangeUtil {
    private final ExpreseDataService eds;
    private final SubjectParameterService sps;
    private final ProductionService ps;

    @Autowired
    public ExpressDataChange(ExpreseDataService eds, SubjectParameterService sps, ProductionService ps) {
        this.eds = eds;
        this.sps = sps;
        this.ps = ps;
    }

    /**
     * 科目间计算
     *
     * @param str subjectId
     */
    String dataChangeBySubject(String str, ExpreseData entity) {
        ExpreseData ed = new ExpreseData();
        ed.setSubjectId(str);
        ed.setActualDate(entity.getActualDate());
        ed.setFinancialExpressFormation(new FinancialExpressFormation());
        ed = (ExpreseData) eds.selectSelective(ed).get(0);
        return ed.getActualData().toString();
    }

    String dataChangeByProduction(String str, String queryDate) {
        Integer tId = Integer.valueOf(deleteFirstChar(str));
        Production p = ps.selectBySelective(tId, queryDate).get(0);
        return p.getActualData().toString();
    }

    String dataChangeByParameter(String str) {
        Integer tId = Integer.valueOf(deleteFirstChar(str));
        SubjectParameter sp = sps.selectByPrimaryKey(tId);
        return sp.getParamValue().toString();
    }

    String deleteFirstChar(String str) {
        StringBuffer sb = new StringBuffer(str);
        sb.deleteCharAt(0);
        return sb.toString();
    }

    /**
     * 根据科目的属性判使用哪种方法转换。缺省为科目表转换，p:生产表转换，S:标准表转换
     */
    @Override
    public String dataChange(String str, Object entity) {
        String sResult = null;
        switch (str.charAt(0)) {
            case 'S':
            case 's':
                sResult = dataChangeByParameter(str);
                break;
            case 'P':
            case 'p':
                sResult = dataChangeByProduction(str, ((ExpreseData) entity).getActualDate());
                break;
            default:
                sResult = dataChangeBySubject(str, (ExpreseData) entity);
        }
        return sResult;
    }

    /**
     * 数据初始化
     *
     * @param queryDate
     */
    public void expressDataInit(String queryDate) {
        eds.expreseDataInit(queryDate);
        ExpreseData ed = new ExpreseData();
        ed.setActualDate(queryDate);
        List<ExpreseData> edList = eds.selectSelective(ed);
    }

}
